package com.aischool.eduorg.api;

import com.aischool.common.model.AiClass;
import com.aischool.params.AiClassParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiClassService;
import com.aischool.service.AiParentService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.aischool.utils.StateEnum;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/api/eduorg/class")
public class ClassController extends Controller {
	
	@Inject
	AiClassService aiClassService;
	@Inject
	AiParentService aiParentService;

	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		AiClassParams params = new AiClassParams();
		
		int org_id = this.getAttrForInt("login_org_id");
				
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));

		params.setState(getInt("state", StateEnum.ALL.getState()));
		params.setOrgId(org_id);
		
		params.setKeywords(get("keywords",""));
		
		// 获取分页
		Object pageData = aiClassService.getPage(params);
		
		jr.setDicKV("pageData", pageData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	/**
	 * 获取单个信息
	 */
	public void getOne() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int class_id = getParaToInt("id",0);
		
		if (class_id > 0) {
			Object obj = aiClassService.getOne(class_id);
			if (obj != null) { jr.setDicKV("oneinfo", obj); }
		}

		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void save() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int user_id = this.getAttrForInt("login_user_id");
		int org_id = this.getAttrForInt("login_org_id");
		// 表单转换类
		int class_id = getParaToInt("class_id", 0);
		AiClass info = getModel(AiClass.class, "", true);

		if (StrKit.isBlank(info.getClassName())) {
			jr.appendMsg("请输入班级名称！");
		}else {
			
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			
			boolean bf = false;
			if (class_id >0) {
				Record rd = aiClassService.getOne(class_id);
				if (rd != null && rd.getInt("org_id") == org_id) {
					// 更新
					bf = info.update();
				}
			}else {
				// 插入
				info.setOrgId(org_id);
				info.setAddUserid(user_id);
				info.setAddTime(DateUtil.getTimestamp());
				bf = info.save();
			}
			
			if (bf) {
				aiClassService.removeCache(class_id);
				aiParentService.removeCacheByClassId(class_id);
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		renderJson(jr.Dic);
	}
	
	public void del() {
		// 结果类
		JsonResult jr = new JsonResult();
		boolean bf = false;

		int org_id = this.getAttrForInt("login_org_id");
		// 表单转换类
		int class_id = getParaToInt("id", 0);
		if (class_id > 0) {
			bf = aiClassService.delForOrg(org_id, class_id);
		}else {
			String ids = get("ids","");
			bf = aiClassService.delMoreForOrg(org_id, ids);
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
}
